﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net.Sockets;
using System.Net;

using System.Text.RegularExpressions;
using System.Security.Cryptography;

using DBA;

namespace PathRunAirSpace
{
    public partial class frmLoadGame : Form
    {
        CDBAdmin DataBase;
        bool[] okRegistre = new bool[4];
        bool login;

        public frmLoadGame()
        {            
            InitializeComponent();
            DataBase = new CDBAdmin("Facundo-VM\\SQLEXPRESS", "ASD_DB");
            DataBase.Conectar();
            DataBase.Desconectar();

            txtPassowrd2.PasswordChar = '☺';
            txtPassword.PasswordChar = '☺';
            txtPassLog.PasswordChar = '☺';

            okRegistre[0] = false;
            okRegistre[1] = false;
            okRegistre[2] = false;
            okRegistre[3] = false;

            txtUsuario.SelectAll();

            System.IO.StreamReader file = new System.IO.StreamReader(@"Config.ini");
            txtPathGame.Text = file.ReadLine();
            file.Close();

            login = false;
            ((Control)tbJuego).Enabled = false;
       }        

        private void frmLoadGame_Load(object sender, EventArgs e)
        {
            Splash form = new Splash();
            form.ShowDialog();
           
            IPHostEntry host = Dns.Resolve(Dns.GetHostName());            
            for (int i = 0; i < host.AddressList.Length; i++)
            {
                cmbRedes.Items.Add(host.AddressList[i]);
            }
        }

        private void btnCrearSV_Click(object sender, EventArgs e)
        {
            string line = txtPathGame.Text;

            try
            {
                System.Diagnostics.Process appy = new System.Diagnostics.Process();
                appy.StartInfo.FileName = line;
                appy.StartInfo.Arguments = "-S " + cmbRedes.SelectedItem.ToString();
                appy.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al Ejecutar path - Informe: " + ex.ToString() , "Error controlado", MessageBoxButtons.OK, MessageBoxIcon.Error);               
            }
        }

        private void btnIngresarSV_Click(object sender, EventArgs e)
        {
            //System.IO.StreamReader file = new System.IO.StreamReader(@"Config.ini");
            string line = txtPathGame.Text;

            try
            {
                System.Diagnostics.Process appy = new System.Diagnostics.Process();
                appy.StartInfo.FileName = line;
                appy.StartInfo.Arguments = "-C " + txtOct1.Text + "." + txtOct2.Text + "." + txtOc3.Text + "." + txtOct4.Text;
                appy.Start();
         
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al Ejecutar path - Informe: " + ex.ToString(), "Error controlado", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtUsuario_Leave(object sender, EventArgs e)
        {
            DataBase.Conectar();

            DataSet data = DataBase.ExecuteQry("SELECT COUNT(*) FROM Usuarios WHERE Usuario like '" +txtUsuario.Text+"'");

            if (int.Parse(data.Tables[0].Rows[0][0].ToString()) == 0 && txtUsuario.Text.Trim() != "")
            {
                imgUsr.Image = PathRunAirSpace.Properties.Resources.chek;
                txtUsuario.BackColor = Color.AliceBlue;
                lblErroMsg.Text = "";

                DataBase.Desconectar();
                okRegistre[0] = true;
            }else
            {
                imgUsr.Image = PathRunAirSpace.Properties.Resources.error;
                txtUsuario.BackColor = Color.Salmon;
                lblErroMsg.Text = "El Usuario ya se encuentra registrado o es un nombre invalido";
                txtUsuario.SelectAll();
                DataBase.Desconectar();
                okRegistre[0] = false;
            }

        }

        private void txtEmail_Leave(object sender, EventArgs e)
        {
            DataBase.Conectar();

            DataSet data = DataBase.ExecuteQry("SELECT COUNT(*) FROM Usuarios WHERE Email like '" + txtEmail.Text + "'");

            Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            if (int.Parse(data.Tables[0].Rows[0][0].ToString()) == 0 && txtEmail.Text.Trim() != "" && regex.IsMatch(txtEmail.Text))
            {
                imgEmail.Image = PathRunAirSpace.Properties.Resources.chek;
                txtEmail.BackColor = Color.AliceBlue;
                lblErroMsg.Text = "";

                DataBase.Desconectar();
                okRegistre[1] = true;
            }
            else
            {
                imgEmail.Image = PathRunAirSpace.Properties.Resources.error;
                txtEmail.BackColor = Color.Salmon;
                lblErroMsg.Text = "El Email ya se encuentra registrado o es un Email invalido";
                
                txtEmail.SelectAll();

                DataBase.Desconectar();

                okRegistre[1] = false;
            }
        }

        private void txtPassword_Leave(object sender, EventArgs e)
        {
            DataBase.Conectar();           

            if (txtPassword.Text.Trim() != "" && txtPassword.TextLength > 7)
            {
                imgPw.Image = PathRunAirSpace.Properties.Resources.chek;
                txtPassword.BackColor = Color.AliceBlue;
                lblErroMsg.Text = "";

                DataBase.Desconectar();
                okRegistre[2] = true;
            }
            else
            {
                imgPw2.Image = PathRunAirSpace.Properties.Resources.error;
                txtPassword.BackColor = Color.Salmon;
                lblErroMsg.Text = "El password tiene que ser mayor a 8 caracteres";

                txtPassword.SelectAll();

                DataBase.Desconectar();

                okRegistre[2] = false;
            }
        }

        private void txtPassowrd2_Leave(object sender, EventArgs e)
        {
            DataBase.Conectar();

            if (txtPassowrd2.Text.Trim() != "" && txtPassowrd2.TextLength > 7 && txtPassowrd2.Text == txtPassword.Text)
            {
                imgPw2.Image = PathRunAirSpace.Properties.Resources.chek;
                txtPassowrd2.BackColor = Color.AliceBlue;
                lblErroMsg.Text = "";

                DataBase.Desconectar();
                okRegistre[3] = true;
            }
            else
            {
                imgPw2.Image = PathRunAirSpace.Properties.Resources.error;
                txtPassowrd2.BackColor = Color.Salmon;
                lblErroMsg.Text = "El password es incorrecto o no coninciden";

                txtPassowrd2.SelectAll();

                DataBase.Desconectar();

                okRegistre[3] = false;
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            if (okRegistre[3])
            {
                txtPassowrd2_Leave(sender, e);
            }
        }

        private void btnRegistrar_Click(object sender, EventArgs e)
        {
            if (okRegistre[0] == true && okRegistre[1] == true && okRegistre[2] == true && okRegistre[3] == true)
            {
                DataBase.Conectar();                    

                DataBase.ExecuteQry("INSERT INTO ASD_DB.dbo.Usuarios(Usuario, Contrasenia, Email, Loging) values ('"+txtUsuario.Text+"', '"+ txtPassword.Text+"', '"+txtEmail.Text +"', 0)");

                DataBase.Desconectar();

                MessageBox.Show("Registrado Con Exito !", "Registrado Con Exito!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                txtPassword.Text = txtUsuario.Text = txtEmail.Text = txtPassowrd2.Text = "";
                okRegistre[0] = false;
                okRegistre[1] = false;
                okRegistre[2] = false;
                okRegistre[3] = false;

            }
            else
            {
                lblErroMsg.Text = "Existen aun datos erroneos o faltantes!";
            }
        }

        private void btnIngresar_Click(object sender, EventArgs e)
        {
            DataBase.Conectar();

            string Query = "SELECT COUNT(*) FROM Usuarios WHERE Usuario like '"+txtUsuarioLog.Text +"' and Contrasenia like '"+txtPassLog.Text +"'";

            DataSet data = DataBase.ExecuteQry(Query);

            if (int.Parse(data.Tables[0].Rows[0][0].ToString()) == 1)
            {
                MessageBox.Show("Bienvenido !");
                login = true;

                tbJuego.Focus();
                ((Control)tbUsuarios).Enabled = false;
                ((Control)tbJuego).Enabled = true;
            }else
            {
                MessageBox.Show("Erro, usuario o contraseña incorrectas");               
            }

            DataBase.Desconectar();
        }

        private void btnOlvideClave_Click(object sender, EventArgs e)
        {

        }
    }
}
